<?php
class Newsdao extends Controller{
	
	public static function GetList(){
		$criteria=new CDbCriteria;
		$criteria->order = 'ngaytao DESC';
		return News::model()->findAll($criteria);
	}
	
	public static function Insert($model){
		$model->ngaytao = DateUtils::getNow();		
		if($model->validate()){
			if($model->save()){
				Newsdao::saveIcon($model);
				return true;
			}
		}
		return false;
	}
	
	public static function Update($model){
		$model->ngaytao = DateUtils::getNow();		
		if(isset($model->Image)){
			unlink(MyConstans::getRootPhysicalPath(). 'images/news/' . $model->hinhanh);
		}
		if($model->save()){
			Newsdao::saveIcon($model);
			return true;
		}
		return false;
	}
	
	public static function saveIcon($item){
		if($item->Image != null){
			$name =  $item->id.'.'. $item->Image->getExtensionName();
			$item->Image->saveAs(MyConstans::getRootPhysicalPath(). 'images/news/'. $name);
			$item->hinhanh = $name;
			$item->update();			
			return true;
		}	
		return false;
	}
	
public static function GetListNewsUpdate(){
		$criteria=new CDbCriteria;
		$criteria->order = 'ngaytao DESC';
		$criteria->limit = 9;
		return News::model()->findAll($criteria);
	}
	
}